<?php
namespace Client\Controller;
use Think\Image;

use Think\Page;

use Think\Controller;
class ScenicOrderController extends Controller {
	public function addOrder(){
		if(IS_POST){
			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$order = D('ScenicOrder');
			$data = $order->create($_POST);
			$ticket = D('ScenicTicket');
			$where['id'] = $_POST['ticket_id'];
			$arr = $ticket->where($where)->find();
			$data['scenic_id'] = $arr['scenic_id'];
			
			$arr = $order->add($data);
			file_put_contents("Public/Log/log.txt", "arr:".json_encode($arr)."\r\n",FILE_APPEND|LOCK_EX);
			if($arr){
				$json['result'] = "0";
				$json['msg'] = "添加成功";
				$json['scenic_id'] = $data['scenic_id'];
			}else{
				$json['result'] = "-1";
				$json['msg'] = "添加失败";
			}
			$this->ajaxReturn($json);
		}
	}
	public function code(){
		vendor("phpqrcode.phpqrcode");
		$data = 'http://www.baidu.com';
		// 纠错级别：L、M、Q、H
		$level = 'L';
		// 点的大小：1到10,用于手机端4就可以了
		$size = 4;
		// 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
		//$path = "images/";
		// 生成的文件名
		//$fileName = $path.$size.'.png';
		\QRcode::png($data, false, $level, $size);
	}
	public function getOrder() {
		if(IS_POST){
//			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$id = $_POST['id'];
			$order = D('ScenicOrder');
			$where['account_id'] = $id;
			if(isset($_POST['type'])){
				$where['type'] = $_POST['type'];
			}
			$result = $order->relation(true)->where($where)->select();
//			$sql=$order->getLastSql();
			$page_num = $_POST['page_num'];
			$start = $page_num * 10;
			$list = array_slice($result,$start,10);
				
			$json['result']="0";
			$json['msg']="获取成功";
				
			if(sizeof($list)>0){
				foreach($list as $k=>$v){
					$list[$k]['createtime']=date("Y-m-d H:i:s",$v['createtime']);
				}
				$json['result'] = '0';
				$json['orders'] = $list;
			}else{
				$json['result'] = '-1';
				$json['msg'] = "没有订单";
			}
//			var_dump($json);
//			file_put_contents("Public/Log/log.txt", "json:".json_encode($json)."\r\n",FILE_APPEND|LOCK_EX);
//			file_put_contents("Public/Log/log.txt", "sql:".$sql."\r\n",FILE_APPEND|LOCK_EX);
			
			$this->ajaxReturn($json);
		}
	}
}